package com.industrial.white.handler;

import com.alibaba.excel.util.StringUtils;
import com.industrial.common.core.utils.SpringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.data.redis.core.RedisTemplate;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class MybatisAdHandler extends BaseTypeHandler<String> {

    private RedisTemplate<String, String> redisTemplate;

    public MybatisAdHandler(){
        this.redisTemplate = SpringUtils.getBean("redisTemplate");
    }

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {

    }

    @Override
    public String getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String ad = resultSet.getString(s);
        if(StringUtils.isBlank(ad)){
            return "";
        }
        String adn = (String)redisTemplate.opsForHash().get("adnbaseid", ad);
        return adn;
    }

    @Override
    public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String ad = resultSet.getString(i);
        if(StringUtils.isBlank(ad)){
            return "";
        }
        String adn = (String)redisTemplate.opsForHash().get("adnbaseid", ad);
        return adn;
    }

    @Override
    public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String ad = callableStatement.getString(i);
        if(StringUtils.isBlank(ad)){
            return "";
        }
        String adn = (String)redisTemplate.opsForHash().get("adnbaseid", ad);
        return adn;
    }
}
